import Seat from "./Seat";

const {ccclass, property} = cc._decorator;

/**游戏结束后的控制脚本 */
@ccclass
export default class GameResult extends cc.Component {

    /**游戏结束后显示的战绩的容器 */
    gameResult: cc.Node = null;
    
    /**四个玩家的战绩的Seat脚本的集合 */
    seats: Seat[] = [];

    onLoad() {
        if(!cc.vv){
            return;
        }

        this.gameResult = this.node.getChildByName('game_result');
        const seats = this.gameResult.getChildByName('seats');
        seats.children.forEach((item, index) => {
            this.seats.push(item.getComponent(Seat));
        });

        /**战绩左上角的返回大厅的按钮 */
        const btnClose = cc.find('Canvas/game_result/btnClose');
        if(btnClose){
            cc.vv.utils.addClickEvent(btnClose, this.node, 'GameResult', 'onBtnCloseClicked');
        }

        /**战绩下面的分享按钮 */
        const btnShare = cc.find('Canvas/game_result/btnShare');
        if(btnShare){
            cc.vv.utils.addClickEvent(btnShare, this.node, 'GameResult', 'onBtnShareClicked');
        }

        this.node.on(cc.vv.game_event_map.GAME_END, (data) => {
            this.onGameEnd(data.detail);
        });
    }


    /** 战绩左上角的返回大厅的按钮的点击事件（返回到游戏大厅）*/
    onBtnCloseClicked(){
        cc.vv.wc.show('正在返回游戏大厅');
        cc.director.loadScene('hall');
    }


    /**更新游戏战绩信息 */
    onGameEnd(endInfo: cc.seat[]){
        const seats = cc.vv.gameNetMgr.seats;
        let maxScore = -1;
        let maxDianPao = 0;
        let dianPaoGaoShou = -1;

        seats.forEach((seat, index) => {
            if(seat.score > maxScore){
                maxScore = seat.score;
            }
            if(endInfo[index].numDianPao > maxDianPao){
                maxDianPao = endInfo[index].numDianPao;
                dianPaoGaoShou = index;
            }
        });

        seats.forEach((seat, index) => {
            let isBigWin = false;
            if(seat.score > 0){
                isBigWin = seat.score == maxScore;
            }
            this.seats[index].setInfo(seat.name, seat.score, isBigWin);
            this.seats[index].setID(seat.userId);
            const isZuiJiaPaoShou = dianPaoGaoShou == index;
            this.showResult(this.seats[index], endInfo[index], isZuiJiaPaoShou);
        });
    }

    showResult(seat: Seat, info: cc.seat, isZuiJiaPaoShou: boolean){
        seat.node.getChildByName('zuijiapaoshou').active = isZuiJiaPaoShou;
        seat.node.getChildByName('zimocishu').getComponent(cc.Label).string = info.numZiMo;
        seat.node.getChildByName('jiepaocishu').getComponent(cc.Label).string = info.numJiePao;
        seat.node.getChildByName('angangcishu').getComponent(cc.Label).string = info.numAnGang;
        seat.node.getChildByName('minggangcishu').getComponent(cc.Label).string = info.numMingGang;
        seat.node.getChildByName('chajiaocishu').getComponent(cc.Label).string = info.numChaJiao;
    }

    /**分享按钮的点击事件 */
    onBtnShareClicked(){
        cc.vv.anysdkMgr.shareResult();
    }
}
